﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Common.Library;
using System.ComponentModel;
using OnlineVideos.Data;
using Microsoft.Phone.Controls;
using OnlineVideos.UI;
using OnlineVideos.Common;
using OnlineVideos.Utilities;
using Common.Utilities;
using OnlineVideos.Entities;

namespace OnlineVideos
{
    public partial class QuizHistory : UserControl
    {
        List<QuizList> objSubjectList;
        private bool IsDataLoaded;
        public QuizHistory()
        {
            InitializeComponent();
            IsDataLoaded = false;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (IsDataLoaded == false)
            {
                GetPageDataInBackground();
                IsDataLoaded = true;
                PageHelper.RemoveEntryFromBackStack("QuizHistory");
            }

        }

        private void GetPageDataInBackground()
        {
            BackgroundHelper bwHelper = new BackgroundHelper();

            bwHelper.AddBackgroundTask(
                                        (object s, DoWorkEventArgs a) =>
                                        {

                                            a.Result = QuizManager.GetsubjectsHistory();
                                        },
                                        (object s, RunWorkerCompletedEventArgs a) =>
                                        {
                                            objSubjectList = (List<QuizList>)a.Result;
                                            if (objSubjectList.Count > 0)
                                            {
                                                lbxSubjectsList.Visibility = Visibility.Visible;
                                                lbxSubjectsList.ItemsSource = objSubjectList;
                                                tblksubjectstest.Visibility = Visibility.Collapsed;
                                            }
                                            else
                                            {
                                                tblksubjectstest.Text = "No " + AppResources.ShowQuizPivotTitle as string + " available";
                                                tblksubjectstest.Visibility = Visibility.Visible;
                                                lbxSubjectsList.Visibility = Visibility.Collapsed;
                                            }

                                        }
                                      );

            bwHelper.RunBackgroundWorkers();


        }

        private void lbxSubjectsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxSubjectsList.SelectedIndex == -1)
                    return;
                AppSettings.ShowID = (lbxSubjectsList.SelectedItem as QuizList).ShowID.ToString();
                AppState.searchtitle = (lbxSubjectsList.SelectedItem as QuizList).Name;
                (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(NavigationHelper.getSubjectDetailPage((lbxSubjectsList.SelectedItem as QuizList), "true"));

            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in lbxSubjectsList_SelectionChanged Method In Favorites file.", ex);
            }
        } 
    }
}
